package order.controller;


import com.example.entity.*;
import com.example.utils.*;
import order.service.ShippingOrderService;
import order.service.TruckOrderService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RefreshScope
public class OrderController {

    @Resource
    ShippingOrderService shippingOrderService;
    @Resource
    TruckOrderService truckOrderService;
    ResultCode resultCode = new ResultCode();


    @GetMapping("/shippingOrderList")
    public Result getShippingOrderList(@RequestParam Map<String, Object> params){
        Long id = null;
        Integer state = null;
        Long carrierId = null;
        if(params.get("id")!=null)
            id = Long.parseLong(params.get("id").toString());
        if(params.get("state")!=null)
            state = Integer.parseInt(params.get("state").toString());
        if(params.get("carrierId")!=null)
            carrierId = Long.parseLong(params.get("carrierId").toString());
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<ShippingOrder> list = shippingOrderService.getShippingOrder(id,state,carrierId);
            map.put("shippingOrderList",list);
            res.setMessage("货运单返回成功");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }

    //创建货运单
    @PostMapping("createShippingOrder")
    public Result createShippingOrder(@RequestBody ShippingOrder s){
        Result res = new Result();
        try {
            shippingOrderService.createOrder(s);
            res.setMessage("创建货运单成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.InfoAddERR);
            res.setMessage(resultCode.getMsg(ResultCode.InfoAddERR));
        }
        return res;
    }


    // 删除货运单
    @DeleteMapping("/delShippingOrder")
    public Result delShippingOrder(@RequestParam Long id){
        Result res = new Result();
        try {
            shippingOrderService.delete(id);
            res.setMessage("货运单删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.InfoDelERR);
            res.setMessage(resultCode.getMsg(ResultCode.InfoDelERR));
        }
        return res;
    }



    // 货运单签收/结算操作
    // 修改货运单状态并添加任务记录
    @PostMapping("/accountOperate")
    public Result accountOperate(@Validated @RequestBody  Map<String,Object> params){
        Integer state = (Integer) params.get("state");//8-签收，9-结算
        Long id = Long.parseLong(params.get("orderId").toString());
        Long operator = Long.parseLong(params.get("operator").toString());
        Result res = new Result();
        try {
            String r = shippingOrderService.changeState(null,state, id, operator);
            res.setMessage(r);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.OpERR);
            res.setMessage(resultCode.getMsg(ResultCode.OpERR));
        }
        return res;
    }

    //接单货运单
    @PostMapping("/takeOrder")
//    @PreAuthorize("hasAnyAuthority('carrier')")
    public Result takeOrder(@Validated @RequestBody Map<String,Object> params) {
        System.out.println("carrierOperate");
        Long carrierId = Long.parseLong(params.get("carrierId").toString());
        Long orderId = Long.parseLong(params.get("orderId").toString());
        Long operator = Long.parseLong(params.get("operator").toString());
        Result res = new Result();
        try {
            shippingOrderService.changeState(carrierId,1,orderId,operator);
            res.setMessage("接单成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoModERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoModERR));
        }

        return res;
    }

    //创建派车单
    @PostMapping("/createTruckOrder")
//    @PreAuthorize("hasAnyAuthority('admin','carrier')")
    public Result createTruckOrder(@RequestBody TruckOrder t){
        Result res = new Result();
        try {
            shippingOrderService.createTruckOrder(t);
            res.setMessage("创建派车单成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoAddERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoAddERR));
        }
        return res;
    }


    @GetMapping("/getTruckOrder")
//    @PreAuthorize("hasAnyAuthority('admin','driver')")
    public Result getTruckOrder(@RequestParam Long id){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<TruckOrder> list = truckOrderService.getTruckOrder(id);
            map.put("truckOrderList",list);
            res.setMessage("返回成功");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }

    // 货运单签收/结算操作
    // 修改货运单状态并添加任务记录
    @PostMapping("/driverOperate")
//    @PreAuthorize("hasAnyAuthority('admin','driver')")
    public Result driverOperate(@Validated @RequestBody  Map<String,Object> params){
        Integer state = (Integer) params.get("state");//3-揽收，4发车,5-到达新位置,6-到达,7-上报意外情况
        Long id = Long.parseLong(params.get("id").toString());
        Long operator = Long.parseLong(params.get("operator").toString());
        String location ="";
        String unexpectedInfo ="";
        Double unexpectedPrice = 0.0;
        if(params.get("location")!=null)
            location  = (String) params.get("location");
        if(params.get("unexpectedInfo")!=null)
            unexpectedInfo = (String) params.get("unexpectedInfo");
        if(params.get("unexpectedPrice")!=null)
            unexpectedPrice = Double.parseDouble(params.get("unexpectedPrice").toString());
        Result res = new Result();
        try {
            String r = truckOrderService.changeState(state, id, operator, location,unexpectedInfo,unexpectedPrice);
            res.setMessage(r);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.OpERR);
            res.setMessage(resultCode.getMsg(ResultCode.OpERR));
        }
        return res;
    }

}
